package com.paradigmcreatives.game.ui;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.paradigmcreatives.game.manager.UIManager;
import com.paradigmcreatives.game.manager.UIManager.AppStates;
import com.paradigmcreatives.game.prefs.Preferences;

public class DoodleCanvas extends View {

	private static final float MINP = 0.25f;
	private static final float MAXP = 0.75f;

	private Bitmap  mBitmap;
	private Canvas  mCanvas;
	private Path    mPath;
	private Paint   mBitmapPaint;
	private Paint   mPaint;
	private boolean erase;
    private int mActivePointerId = -1;
    private int secondPointerID;
    private int color = Color.RED;
	UIManager manager = null;
	
	private boolean enableDrawing = false;

	public boolean isEnableDrawing() {
		return enableDrawing;
	}

	public void setEnableDrawing(boolean enableDrawing) {
		if(enableDrawing == false){
			touch_up();
		}
		this.enableDrawing = enableDrawing;
	}
	
	public void setPaintObject(Paint mPaint) {
		this.mPaint = mPaint;

	}

	public DoodleCanvas(Context c,AttributeSet attributeSet) {
		super(c,attributeSet);
		
		mPath = new Path();
		mPaint = new Paint();
		mPaint.setAntiAlias(true);
		mPaint.setDither(true);
		mPaint.setColor(0xFFFF0000);
		mPaint.setStyle(Paint.Style.STROKE);
		mPaint.setStrokeJoin(Paint.Join.ROUND);
		mPaint.setStrokeCap(Paint.Cap.ROUND);
		mPaint.setStrokeWidth(12);
		mBitmapPaint = new Paint(Paint.DITHER_FLAG);
	}

	@Override
	protected void onSizeChanged(int w, int h, int oldw, int oldh) {
		super.onSizeChanged(w, h, oldw, oldh);
		mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
		mCanvas = new Canvas(mBitmap);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		canvas.drawColor(0xFFFFFFFF);

		canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);

		canvas.drawPath(mPath, mPaint);
	}

	private float mX, mY;
	private static final float TOUCH_TOLERANCE = 4;

	private void touch_start(float x, float y) {
		if(manager != null && manager.appState != AppStates.STARTED_DRAWING){
			manager.startedDrawing();
		}
		manager.isDrawing(true);

		mPath.reset();
		mPath.moveTo(x, y);
		mX = x;
		mY = y;
	}
	private void touch_move(float x, float y) {
		float dx = Math.abs(x - mX);
		float dy = Math.abs(y - mY);
		if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
			mPath.quadTo(mX, mY, (x + mX)/2, (y + mY)/2);
			mX = x;
			mY = y;
		}
	}
	private void touch_up() {
		//mPath.lineTo(mX, mY);
		// commit the path to our offscreen
		manager.isDrawing(false);
		mCanvas.drawPath(mPath, mPaint);
		// kill this so we don't double draw
		mPath.reset();
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if(enableDrawing){
		float x = event.getX();
		float y = event.getY() - Preferences.offset;
			switch (event.getAction() & MotionEvent.ACTION_MASK) {
			case MotionEvent.ACTION_DOWN:
				touch_start(x, y);
				invalidate();
				 // Save the ID of this pointer
                mActivePointerId = event.getPointerId(0);
				break;
			case MotionEvent.ACTION_MOVE:
				if(!erase){
                    touch_move(x, y);
                	}else{
                		float x1 = event.getX(0);
                		float y1 = event.getY(0);
                		float x2 = event.getX(1);
                		float y2 = event.getY(1);
                		
                		mPath.moveTo(x1, y1);
                		mPath.lineTo(x2, y2);
                		
                	}
				invalidate();
				break;
			case MotionEvent.ACTION_UP:
				erase = false;
            	mActivePointerId = -1;
            	touch_up();
            	mPaint.setStrokeWidth(15);
            	mPaint.setColor(color);
                invalidate();
				break;
			case MotionEvent.ACTION_POINTER_DOWN:
            	erase = true;
            	//firstPointerID = event.getPointerId(0);
            	secondPointerID = event.getPointerId(1);
            	//mCanvas.drawPath(mPath, mPaint);
                mPath.reset();
                color = mPaint.getColor();
            	mPaint.setColor(Color.WHITE);
            	//mPaint.setStyle(Style.FILL);
            	mPaint.setStrokeWidth(20);
            	break;
            case MotionEvent.ACTION_POINTER_UP:
            	
            	System.out.println("pointer is up");
            	// Extract the index of the pointer that left the touch sensor
                final int pointerIndex = (event.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) 
                        >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
                final int pointerId = event.getPointerId(pointerIndex);
                if (pointerId == mActivePointerId) {
                    // This was our active pointer going up. Choose a new
                    // active pointer and adjust accordingly.
                    final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
                    mX = event.getX(newPointerIndex);
                    mY = event.getY(newPointerIndex);
                    mActivePointerId = event.getPointerId(newPointerIndex);
                }
            	
            	mCanvas.drawPath(mPath, mPaint);
                mPath.reset();
                
            	//mX = event.getX(0);
            	//mY = event.getY(0);
            	mPath.moveTo(mX, mY);
            	break;
			}
		}
		invalidate();
		return true;
	}

	public void registerToListen(UIManager uiManager) {
		this.manager = uiManager;
	}
	
	public void setPaintColor(int color){
				
		mPaint.setColor(color);
	}

	
}